package application.common.cron4j;

import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.cron4j.ITask;

import application.common.model.FaultBox;
import application.common.render.HttpClientResult;
import application.common.util.HttpUtil;
import application.common.util.ShiroUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class FaultTask implements ITask{
	protected final Log log = Log.getLog(getClass());

	private boolean isEnd=true;
	
	public void run() {
		if(isEnd) {
			try {
				if(ShiroUtil.getConfig()!=null && !StrKit.isBlank(ShiroUtil.getConfig().getSynURL())) {
					isEnd=false;
					try {
						HttpClientResult httpClientResult=HttpUtil.doGet(String.format("%s/api/cab/getFault?cabinetId=%s", ShiroUtil.getConfig().getSynURL(),ShiroUtil.getAuth()));
						if(httpClientResult==null) {
							throw new Exception("请求异常！");
						}
						if(httpClientResult.getCode()!=200) {
							throw new Exception(httpClientResult.getContent());
						}
						String content=httpClientResult.getContent();
						if(StrKit.isBlank(content)) {
							throw new Exception("未返回数据！");
						}
						JSONObject jsonObject=JSONObject.fromObject(content);
						if(jsonObject==null) {
							throw new Exception("未返回数据！");
						}
						if(jsonObject.has("code")) {
							int code=jsonObject.getInt("code");
							if(code!=200) {
								throw new Exception(jsonObject.getString("message"));
							}
							if(jsonObject.has("data")) {
								String data=jsonObject.getString("data");
								if(StrKit.isBlank(data)) {
									Db.delete("delete from "+FaultBox.TABLE_NAME);
									throw new Exception("无故障数据");
								}
								JSONArray array=JSONArray.fromObject(data);
								if(array==null || array.size()==0) {
									Db.delete("delete from "+FaultBox.TABLE_NAME);
									throw new Exception("无故障数据");
								}
								FaultBox faultBox;
								StringBuffer sb=new StringBuffer();
								for(Object object:array) {
									JSONObject row=JSONObject.fromObject(object);
									if(row!=null) {
										String id=row.getString("id");
										String moduleId=row.getString("moduleId");
										String boardId=row.getString("boardId");
										int boxNo=row.getInt("boxNo");
										faultBox=FaultBox.dao.findById(id);
										if(faultBox!=null) {
											faultBox.setBoardId(boardId);
											faultBox.setBoxNo(boxNo);
											faultBox.setCabinetId(moduleId);
											faultBox.update();
										}else {
											faultBox=new FaultBox();
											faultBox.setId(id);
											faultBox.setBoardId(boardId);
											faultBox.setBoxNo(boxNo);
											faultBox.setCabinetId(moduleId);
											faultBox.save();
										}
										sb.append("'").append(id).append("'").append(",");
									}
								}
								if(!StrKit.isBlank(sb.toString())) {
									Db.delete("delete from "+FaultBox.TABLE_NAME+" where id not in ("+(sb.substring(0,sb.length()-1).toString())+") "); 
								}
								isEnd=true;
							}else {
								isEnd=true;
							}
						}else {
							isEnd=true;
						}
					}catch (Exception e) {
						isEnd=true;
					}
				}else {
					throw new Exception("未配置同步接口地址！");
				}
			}catch (Exception e) {
				log.error("故障同步异常",e);
			}finally {
				isEnd=true;
			}
		}
	}

	public void stop() {
	}

}
